package cc.controller;

import cc.entity.Employee;
import cc.mapper.EmployeeMapper;
import cc.repository.EmployeeRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @author My
 * @date 2021/6/30 14:54
 */
@RestController
@RequestMapping("/employee")
//说明接口文件
@Api(value = "员工管理", tags = "员工管理", description = "员工管理")
public class EmployeeController {

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    EmployeeRepository employeeRepository;

    @GetMapping("/list.do")
    //说明是什么方法(可以理解为方法注释)
    @ApiOperation(value = "查询员工列表", notes = "查询员工列表")
    List<Employee> list(){
        List<Employee> list = employeeMapper.list();
        return list;
    }

    @GetMapping("/getById.do/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询")
    public Employee getById(@PathVariable("id") Integer id) {
        return employeeMapper.getById(id);
    }

    @ApiOperation(value = "新增记录")
    @PostMapping("/add.do")
    public Employee add(@RequestBody Employee one) {
        one.setId(null);

        employeeRepository.save(one);

        // 返回结果是插入影响的行数，如果是1表示插入成功。
        Integer row = employeeMapper.insert(one);
        if(row == 1){
            one = employeeMapper.getById(one.getId());
            return one;
        }
        return null;
    }

    @ApiOperation(value = "修改记录")
    @PostMapping("/update.do")
    public Employee update(@RequestBody Employee one) {
        Boolean result = employeeMapper.update(one);
        if(result == true){
            one = employeeMapper.getById(one.getId());
            return one;
        }
        return null;
    }


}
